package com.dhl.fin.api.service.cronService;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.dhl.fin.api.domain.RemindTask;
import com.dhl.fin.api.enums.RemindDateTypeEnum;
import com.nlf.calendar.Lunar;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author CuiJianbo
 * @since 2025/5/5
 */
@Service
public class EveryYearCron implements RemindCron {

    @Override
    public String calculateCron(RemindTask remindTask) {
        Date remindDate = remindTask.getRemindDate();
        if (remindTask.getDateType().equals(RemindDateTypeEnum.SOLAR.getName())) {
            DateTime now = DateUtil.date();
            String[] remindTime = remindTask.getRemindTime().split(":");
            String[] indexs = remindTask.getLunarDayIndex().split(",");
            Lunar midAutumn = new Lunar(now.year(), Integer.parseInt(indexs[0]) + 1, Integer.parseInt(indexs[1]) + 1);
            DateTime dateTime = DateUtil.parseDate(midAutumn.getSolar().toString());
            if (dateTime.before(now)) {
                midAutumn = new Lunar(now.year() + 1, Integer.parseInt(indexs[0]) + 1, Integer.parseInt(indexs[1]) + 1);
                DateTime nextDateTime = DateUtil.parseDateTime(String.format("%s %s:%s:00", midAutumn.getSolar().toString(), remindTime[0], remindTime[1]));
                remindDate = nextDateTime;
            } else {
                remindDate = dateTime;
            }
        }
        String[] remindTime = remindTask.getRemindTime().split(":");
        int month = DateUtil.month(remindDate) + 1;
        int day = DateUtil.dayOfMonth(remindDate);


        return String.format("0 %s %s %d %d ? ", remindTime[1], remindTime[0], day, month);
    }

}
